package com.dx.mes.service.query.service;

import com.dx.common.table.his.His_ret_tool_st;
import com.dx.mes.common.dao.IHisRetToolStDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.query.service.IHiptolhisService;
import com.dx.mes.facade.query.tx.Hiptolhis.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.*;

/**
 * Created by cmj on 2017/11/18.
 */
@Service("hiptolhisService")
public class HiptolhisService implements IHiptolhisService {
    private LogUtils logUtils;
    private static final String TX_NAME = T_HPTOLHIS;

//    @Resource
//    private SessionFactory sessionFactoryHis;

    @Autowired
	private IHisRetToolStDao hisRetToolStDao;
//	@Autowired
//	private IHisRetToolPtDao hisRetToolPtDao;

	@Override
    public String subMainProc(String evt_no, String strInMsg) {
        AppContext.clear();
        AppContext.setCurrServiceName(HiptolhisService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(HiptolhisService.class);
        logUtils.info("[InTrx:" + strInMsg + "]");
//        Session session = sessionFactoryHis.getCurrentSession();
        String outMsg = "";

        HiptolhisI inTrx = new HiptolhisI();
        HiptolhisO outTrx = new HiptolhisO();

        outTrx.setTrx_id(TX_NAME);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

        try {
            inTrx = JacksonUtil.fromJson(strInMsg, HiptolhisI.class);

            subMainProc2(inTrx.getAction_flg().toCharArray()[0],inTrx,outTrx);
            ;
            if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
//                session.flush();
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (Exception ex) {
            outTrx.setRtn_mesg(ex.toString());
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            logUtils.error(Util.stackTraceToString(ex));
        } finally {
            outMsg = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + outMsg + "]");
        }
        return outMsg;
    }
    private void subMainProc2(char actionType, HiptolhisI inTrx, HiptolhisO outTrx) {

        switch (actionType) {
            case 'T': {
                getHisToolByCondition(inTrx,outTrx);
                break;
            }
            default: {
                String gszMsgBuf = "Invalide type input: [" + actionType + "]";
                outTrx.setRtn_mesg(gszMsgBuf);
                outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT));
            }
        }
    }

    private void getHisToolByCondition(HiptolhisI inTrx, HiptolhisO outTrx) {
        HiptolhisIA iary = inTrx.getIary().get(0);
//        Session session = sessionFactoryHis.openSession();
//		String toolHis_hql = "From His_ret_tool_st where tool_id_fk = '"+iary.getTool_id()+"' and evt_cate <> '"+EVT_TOOL_FETCH+"'";
		String toolHis_hql = "From His_ret_tool_st where tool_id_fk = '"+iary.getTool_id()+"'";
//		List<His_ret_tool_st> his_ret_tool_sts = session.createQuery(toolHis_hql).list();
		List<His_ret_tool_st> his_ret_tool_sts = hisRetToolStDao.find(toolHis_hql);
		List<HiptolhisOA> toolHisList = new ArrayList<HiptolhisOA>();
		for (His_ret_tool_st his_ret_tool_st : his_ret_tool_sts){
//		His_ret_tool_st his_ret_tool_st = his_ret_tool_sts.get(0);
				HiptolhisOA oaryA = new HiptolhisOA();
				oaryA.setTool_id_fk(his_ret_tool_st.getTool_id_fk());
				oaryA.setTool_stat(his_ret_tool_st.getTool_stat());
				oaryA.setTool_id_fk(his_ret_tool_st.getTool_id_fk());
				oaryA.setTool_stat(his_ret_tool_st.getTool_stat());
				oaryA.setTool_sub_stat(his_ret_tool_st.getTool_sub_stat());
				oaryA.setTool_lock_flg(his_ret_tool_st.getTool_lock_flg());
				oaryA.setTool_hold_flg(his_ret_tool_st.getTool_hold_flg());
				oaryA.setTool_run_mode(his_ret_tool_st.getTool_run_mode());
				oaryA.setLd_rtd_flg(his_ret_tool_st.getLd_rtd_flg());
				oaryA.setUl_rtd_flg(his_ret_tool_st.getUl_rtd_flg());
				oaryA.setPv_tool_stat(his_ret_tool_st.getPv_tool_stat());
				oaryA.setPv_tool_sub_stat(his_ret_tool_st.getPv_tool_sub_stat());
				oaryA.setTool_note(his_ret_tool_st.getTool_note());
				oaryA.setEvt_timestamp(his_ret_tool_st.getEvt_timestamp().toString());
				if(null != his_ret_tool_st.getClm_timestamp()){
					oaryA.setClm_timestamp(his_ret_tool_st.getClm_timestamp().toString());
				}
				if(null != his_ret_tool_st.getKeep_time()){
					oaryA.setKeep_time(his_ret_tool_st.getKeep_time().toString());
				}
				if(null != his_ret_tool_st.getUl_last_fetch_timestamp()){
					oaryA.setUl_last_fetch_timestamp(his_ret_tool_st.getUl_last_fetch_timestamp().toString());
				}
				if(null != his_ret_tool_st.getLd_last_fetch_timestamp()){
					oaryA.setLd_last_fetch_timestamp(his_ret_tool_st.getLd_last_fetch_timestamp().toString());
				}
				oaryA.setEvt_usr(his_ret_tool_st.getEvt_usr());
				toolHisList.add(oaryA);
		}

//		String portHis_hql = "FROM His_ret_tool_pt where 1=1 ";
//		if (StringUtils.isNotBlank(iary.getTool_id())) {
//			portHis_hql = portHis_hql + " and tool_id_fk='" + iary.getTool_id() + "'";
//		}
//		if (StringUtils.isNotBlank(iary.getPort_typ())) {
//			portHis_hql = portHis_hql + " and port_typ = '"+ iary.getPort_typ() + "'";
//		}
//		List<His_ret_tool_pt> his_ret_tool_pts = session.createQuery(portHis_hql).list();
//		List<His_ret_tool_pt> his_ret_tool_pts = hisRetToolPtDao.find(portHis_hql);
//		List<HiptolhisOB> ptHisList = new ArrayList<HiptolhisOB>();
//		for (His_ret_tool_pt his_ret_tool_pt : his_ret_tool_pts){
//			HiptolhisOB oaryB = new HiptolhisOB();
//			oaryB.setTool_id_fk(his_ret_tool_pt.getTool_id_fk());
//			oaryB.setPort_id(his_ret_tool_pt.getPort_id());
//			oaryB.setPort_typ(his_ret_tool_pt.getPort_typ());
//			oaryB.setPort_ng_flg(his_ret_tool_pt.getPort_ng_flg());
//			oaryB.setPort_stat(his_ret_tool_pt.getPort_stat());
//			if(null != his_ret_tool_pt.getPort_timestamp()){
//				oaryB.setPort_timestamp(his_ret_tool_pt.getPort_timestamp().toString());
//			}
//			if(null != his_ret_tool_pt.getEvt_timestamp()){
//				oaryB.setEvt_timestamp(his_ret_tool_pt.getEvt_timestamp().toString());
//			}
//			oaryB.setEvt_usr(his_ret_tool_pt.getEvt_usr());
//			ptHisList.add(oaryB);
//		}
//		session.close();
        outTrx.setOary(toolHisList);
//		outTrx.setOaryB(ptHisList);
		outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

}
